package parroty;

import org.apache.commons.lang.StringUtils;

import pojo.GoogleWeatherParser;
import pojo.Weather;
import pojo.WeatherForecast;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;
import com.google.wave.api.*;

public class ParrotyServlet extends AbstractRobotServlet {

	@Override
	public void processEvents(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet();
		if (bundle.wasSelfAdded()) {
			// Blip blip = wavelet.appendBlip();
			// TextView textView = blip.getDocument();
			// textView.append("Wave bot is added!!");
		}

		for (Event e : bundle.getEvents()) {
			if (e.getType() == EventType.BLIP_SUBMITTED) {
				// get the submitted blip and its text
				Blip blip = e.getBlip();
				TextView tv = blip.getDocument();
				String submittedText = tv.getText();
				// someone asks for translation
				if (isAskForTranslation(submittedText)) {
					String sentence = StringUtils.substringAfter(submittedText,
							".t ");
					// Set the HTTP referrer to your website address.
				    Translate.setHttpReferrer("http://wavebotapp.appspot.com/");

				    String translatedText;
					try {
						translatedText = Translate.execute(sentence, Language.ENGLISH, Language.CHINESE_SIMPLIFIED);

						TextView textView = blip.getDocument();
						textView.append(translatedText);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
				// someone asks for weather
				if (isAskForWeather(submittedText)) {
					// create a response blip
					TextView textView = blip.getDocument();
					String city = StringUtils.substringAfter(submittedText,
							".weather ");

					WeatherForecast wf = GoogleWeatherParser.parser(city);

					// current weather
					Weather cw = wf.getCurrent();
					textView.append("\n\n");
					textView.appendMarkup("<b>当前</b>: ");
					Image image = new Image(cw.getIcon(), 40, 40, "");
					textView.appendElement(image);
					textView.append(" " + cw.getCondition());
					textView.appendMarkup(" 温度: " + cw.getCurrentTemp());
					if (StringUtils.isNotEmpty(cw.getWindCondition())) {
						textView.appendMarkup(" " + cw.getWindCondition());
					}
					textView.appendMarkup(" " + cw.getHumidity());

					// today's weather
					cw = wf.getToday();
					textView.append("\n");
					textView.appendMarkup("<b>" + cw.getDayOfWeek() + "</b>: ");
					image = new Image(cw.getIcon(), 40, 40, "");
					textView.appendElement(image);
					textView.appendMarkup(" " + cw.getCondition());
					textView.appendMarkup(" 温度: " + cw.getLow() + " / "
							+ cw.getHigh());

					// tomorrow's weather
					cw = wf.getTomorrow();
					textView.append("\n");
					textView.appendMarkup("<b>" + cw.getDayOfWeek() + "</b>: ");
					image = new Image(cw.getIcon(), 40, 40, "");
					textView.appendElement(image);
					textView.appendMarkup(" " + cw.getCondition());
					textView.appendMarkup(" 温度: " + cw.getLow() + " / "
							+ cw.getHigh());

				}
			}
			if (e.getType() == EventType.WAVELET_PARTICIPANTS_CHANGED) {
				// Blip blip = wavelet.appendBlip();
				// TextView textView = blip.getDocument();
				// textView.append("new participant joins.. welcome..");
			}
		}
	}

	private boolean isAskForWeather(String submittedText) {
		return (StringUtils.startsWith(submittedText, ".weather ") && submittedText
				.length() > ".weather ".length())
				|| (StringUtils.startsWith(submittedText, ".天气 ") && submittedText
						.length() > ".天气 ".length());
	}
	
	private boolean isAskForTranslation(String submittedText) {
		return StringUtils.startsWith(submittedText, ".t ") && submittedText
				.length() > ".t ".length();
	}
}